import numpy as np
from abc import ABC, abstractmethod

class BaseOptimizer(ABC):
    def __init__(self, dim, pop_size=50, max_iter=1000):
        self.dim = dim
        self.pop_size = pop_size
        self.max_iter = max_iter
        self.best_solution = None
        self.best_fitness = float('inf')
        
    @abstractmethod
    def optimize(self, objective_func):
        """优化主循环"""
        pass
        
    def initialize_population(self, bounds):
        """初始化种群"""
        return np.random.uniform(bounds[0], bounds[1], (self.pop_size, self.dim))
        
    def clip_to_bounds(self, x, bounds):
        """将解限制在边界内"""
        return np.clip(x, bounds[0], bounds[1]) 